﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataStorage;
using System.Diagnostics;

namespace Booky.WinFormsClient
{
    public partial class AccountSetupForm : Form
    {
        AsyncFunc<string, bool> authenticateTask;
        AsyncFunc<string, string> signInTask;

        public AccountSetupForm()
        {
            InitializeComponent();
            authenticateTask = new AsyncFunc<string, bool>(AuthenticateTask);
            authenticateTask.Completed += new AsyncFuncCompletedEventHandler<bool>(authenticateTask_Completed);

            signInTask = new AsyncFunc<string, string>(SignInTask);
            signInTask.Completed += new AsyncFuncCompletedEventHandler<string>(signInTask_Completed);
        }

        void authenticateTask_Completed(object sender, AsyncFuncCompletedEventArgs<bool> e)
        {
            if (e.Error != null)
            {
                // Report exception
            }
            else if (e.Cancelled)
            {
                // Ignore
                MessageBox.Show("Operation cancelled");
            }
            else
            {
                //    textBox1.Text = e.Result;
                //close the window
                if(e.Result)
                {
                    signInTask.TryInvokeAsync("What is the Answer to the Ultimate Question of Life, the Universe and Everything");
                }
                else
                {
                    MessageBox.Show("Incorrect Username and/or Password.");
                    txtUsername.Text = "";
                    txtPassword.Text = "";
                }
            }

            //progressBar1.Value = 0;
        }

        void signInTask_Completed(object sender, AsyncFuncCompletedEventArgs<string> e)
        {
            if (e.Error != null)
            {
                // Report exception
            }
            else if (e.Cancelled)
            {
                // Ignore
                MessageBox.Show("Operation cancelled");
            }
            else
            {
                //    textBox1.Text = e.Result;
                //close the window
                this.Close();

                //we should be inserting a row to the list control here rather than updating the entire listcontrol
            }

            //progressBar1.Value = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(errorProvider1.GetError(txtUsername)) && string.IsNullOrEmpty(errorProvider2.GetError(txtPassword)))
                authenticateTask.TryInvokeAsync("What is the Answer to the Ultimate Question of Life, the Universe and Everything");
        }

        private bool AuthenticateTask(string question, Func<bool> isCancelled, Action<int> reportProgress)
        {
            Repository r = new Repository();

            bool isAuth = CloudRepository.AuthenticateStorage(txtUsername.Text, txtPassword.Text);

            //r.SigninBooky(txtUsername.Text, txtPassword.Text);

            return isAuth;
        }

        private string SignInTask(string question, Func<bool> isCancelled, Action<int> reportProgress)
        {
            Repository r = new Repository();

            r.SigninBooky(txtUsername.Text, txtPassword.Text);
            
            //TODO implement the file on app load. 

            FileAssociationUtil fau = new FileAssociationUtil();
            
            fau.CreateBookyFileAssocation();

            return "42";
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                this.errorProvider1.SetError(txtUsername, "Username is required");
            }
            else
            {
                errorProvider1.SetError(txtUsername, "");
            }
        }

        private void txtPassword_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                this.errorProvider1.SetError(txtPassword, "Password is required");
            }
            else
            {
                errorProvider1.SetError(txtPassword, "");
            }
        }

        private void lblCloudMeSignUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start("http://www.cloudme.com/");
            }
            catch (SystemException ex)
            {
                MessageBox.Show("Website could not be loaded");
            }
        }
    }
}
